const ACTION_PREFIX = module.id; // 设置action前缀确保type的唯一性
// console.log('ACTION_PREFIX1', ACTION_PREFIX);

export const INCREMENT = `${ACTION_PREFIX}/INCREMENT`;
export function increment() {
  return { type: INCREMENT };
}

export const DECREMENT = `${ACTION_PREFIX}/DECREMENT`;
export function decrement() {
  return { type: DECREMENT };
}

export const RESET = `${ACTION_PREFIX}/RESET`;
export function reset() {
  return { type: RESET };
}

export const GET_USER_INFO_REQUEST = `${ACTION_PREFIX}/GET_USER_INFO_REQUEST`;
export function getUserInfoRequest() {
  return {
    type: GET_USER_INFO_REQUEST,
  };
}

export const GET_USER_INFO_SUCCESS = `${ACTION_PREFIX}/GET_USER_INFO_SUCCESS`;
export function getUserInfoSuccess(userInfo) {
  return {
    type: GET_USER_INFO_SUCCESS,
    userInfo,
  };
}

export const GET_USER_INFO_FAIL = `${ACTION_PREFIX}/GET_USER_INFO_FAIL`;
export function getUserInfoFail() {
  return {
    type: GET_USER_INFO_FAIL,
  };
}

export function getUserInfo() {
  return async dispatch => {
    dispatch(getUserInfoRequest());

    try {
      const response = await fetch('/api/user.json');
      const json = await response.json();
      dispatch(getUserInfoSuccess(json));
    } catch (e) {
      // dispatch(getUserInfoFail());
      dispatch(getUserInfoSuccess({ name: 'tikeyc' }));
    }
  };
}
